<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\WaterCategory;

class WaterCategoryController extends Controller
{
    protected $message = [
        'name.required' => '分类名称必须',
        'name.unique' => '分类名称已存在',
        'name.max' => '分类名称不能超过50个字符',
    ];
    /**
     * 分类列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('name', '');
        $query = WaterCategory::query();
        if(!empty($name)) {
            $query->where('name', 'like', $name.'%');
        }

        $list = $query->paginate($perPage);
        return $list;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 添加分类
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->input();
        $v = $this->validate2($request, [
            'name'=>'required|unique:water_categories|max:50'
        ], $this->message);
        
        $v->validate();
        $data['slug_name'] = pinyin($data['name']);
        $ret = WaterCategory::create($data);
        if(!$ret){
            $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 更新分类.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->input();
        $v = $this->validate2($request, [
            'name'=>'required|unique:water_categories,name,'.$id.',|max:50'
        ], $this->message);
        
        $v->validate();
        
        $category = WaterCategory::find($id);
        if($category->name!=$data['name']) {
            $data['slug_name'] = pinyin($data['name']);
        }
        $ret = $category->update($data);
        if(!$ret){
            $this->error('保存失败');
        }
    }

    /**
     * 删除分类
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = WaterCategory::findOrFail($id);
        if(!$category->delete()){
            $this->error('删除失败');
        }
    }
    
    /**
     * 获取所有分类
     */
    public function getAllCategory() {
        $info = WaterCategory::select(['id', 'name'])->get();
        return [ 'data'=>$info ];
    }
}
